package day20170929;


import java.io.UnsupportedEncodingException;
import java.util.Scanner;

/**
 * Created by 14394 on 2017/9/29.
 */
public class MyWork {
    public static void main(String[] args)  {
        System.out.println("请输入你的指令");
        System.out.println("1：求和");
        System.out.println("2：求积");
        System.out.println("3：求最大值");
        System.out.println("4：求最大公约数和最小公倍数");
        System.out.println("0：退出");
        int i = new Scanner(System.in).nextInt();
        switch (i) {
            case 1:
                System.out.println("请输入要求和的数");
                String s =new Scanner(System.in).next();
                 int number=s.length();
                 int ints[]=new int[number];
                 for (int j=0;j<number;j++){
                     ints[j]=s.charAt(j)-48;
                 }
                System.out.println(ints.length);
                System.out.println(ints[0]);
                System.out.println("这几个数的和为"+new MyWork().sum(ints));
                break;
            case 2:
                System.out.println("请输入要求积的数");
                String s1 =new Scanner(System.in).next();
                int number1=s1.length();
                int ints1[]=new int[number1];
                for (int j=0;j<number1;j++){
                    ints1[j]=s1.charAt(j)-48;
                }
                System.out.println("这几个数的积为"+new MyWork().multiplication(ints1));
                break;
            case 3:System.out.println("请输入要找的数列");
                String s2 =new Scanner(System.in).nextLine();
                byte[] bytes2= s2.getBytes();
                int[] b2=new int[bytes2.length];
                for (int j=0;j<bytes2.length;j++){
                    b2[j]=new Byte(bytes2[j]).intValue()-48;
                }
                System.out.println("这几个数中最大的为"+new MyWork().maxNum(b2));
                break;
            case 0:
                return;
            default:
                System.out.println("程序完成，白白");
                break;

        }
}

    int sum(int[] ints) {
        int s = 0;
        for (int i = 0; i <= ints.length - 1; i++) {

            s += ints[i];
        }
        return s;
    }

    int multiplication(int ints[]){
        int s = 1;
        for (int i = 0; i <= ints.length - 1; i++) {

            s *= ints[i];
        }
        return s;
    }

    int maxNum(int ints[]){//冒泡算法实际每次只对序列最后一个数起作用
        for (int i=0;i<ints.length-1;i++){  //进行N-1轮的比较
            for (int j=0;j<ints.length-i-1;j++){//每一轮比较前n-1-i次
                if (ints[j]>ints[j+1]){
                    int temp=ints[j];
                    ints[j]=ints[j+1];
                    ints[j+1]=temp;
                }
            }
        }
        return ints[ints.length-1];
    }


}
